<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>概念众包-运营后台</title>
    <link rel="stylesheet" href="./public/bower_components/bootstrap/dist/css/bootstrap.min.css">
    <script src="./public/dist/js/jquery.min.js"></script>
    <script src="./public/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="./public/dist/css/index.css">
    <link rel="stylesheet" href="./public/dist/css/spop.min.css">
    <script src="./public/dist/js/spop.min.js"></script>
    <link rel="stylesheet" href="./public/dist/css/loading.css">
    <script src="./public/dist/js/loading.js"></script>
    <link rel="stylesheet" href="./public/dist/css/AdminLTE.min.css">
    <link rel="stylesheet" href="./public/bower_components/font-awesome/css/font-awesome.min.css">
    <style type="text/css">
        table{
            table-layout: fixed;
        }
        td{
            width: 100%;
            word-break: keep-all;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        tr{
            cursor: pointer;
        }
        .chen-content {
            min-height: 100%;
            background-color: #ecf0f5;
            z-index: 800;
        }
    </style>
</head>
<body>
<div class="chen-content">
    <section class="content-header">
        <h1>
            股票列表
            <small>Control panel</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 正式库</a></li>
            <li class="active">股票列表</li>
        </ol>
    </section>
    <section class="content" style="height: 100%;">
        <div class="form-group form-inline" style="float: right;">
            <input type="text" class="form-control" id="code" placeholder="股票代码" autocomplete="off">
            <input type="text" class="form-control" id="stock_name" placeholder="股票名称" autocomplete="off">
            <button type="button" class="btn btn-info" id="search">查询</button>
        </div>
        <div style="text-align: center;">
            <table class="table table-bordered table-hover table-condensed table-striped">
                <colgroup>
                    <col width="10%">
                    <col width="10%">
                    <col width="10%">
                    <col width="20%">
                    <col width="10%">
                    <col width="10%">
                    <col width="20%">
                    <col width="10%">
                </colgroup>
                <thead>
                <tr style="background-color: #3c8dbc;color: white;">
                    <th>股票代码</th>
                    <th>股票名称</th>
                    <th>机构名称</th>
                    <th>办公地址</th>
                    <th>上市时间</th>
                    <th>法人代表</th>
                    <th>主营业务</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tbody">
                </tbody>
            </table>
            <div class="page_div" id="page" style="margin: 0 auto"></div>
        </div>
    </section>
</div>
<div class="modal fade" id="saveModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel2">编辑股票</h4>
            </div>
            <div class="modal-body">
                <div class="form-horizontal">
                    <input class="form-control" id="id" value="" type="hidden">
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">股票名称</label>
                        <div class="col-sm-8">
                            <input class="form-control" id="name" placeholder="股票名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="org_name" class="col-sm-2 control-label">机构名称</label>
                        <div class="col-sm-8">
                            <input class="form-control" id="org_name" placeholder="机构名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="address" class="col-sm-2 control-label">办公地址</label>
                        <div class="col-sm-8">
                            <input class="form-control" id="address" placeholder="办公地址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="main_business" class="col-sm-2 control-label">主营业务</label>
                        <div class="col-sm-8">
                            <input class="form-control" id="main_business" placeholder="主营业务">
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="stock_save">保存</button>
            </div>
        </div>
    </div>
</div>
</body>
<script type="application/javascript">
    var current = 1;
    $('document').ready(function () {
        doSearch();
        $('#search').click(function () {
            current = 1;
            doSearch();
        });
        $('#tbody').on('click', '#save', function (event) {
            var info = eval($(this).parent().parent().data('json'));
            $('#id').val(info.id);
            $('#name').val(info.name);
            $('#org_name').val(info.org_name);
            $('#address').val(info.address);
            $('#main_business').val(info.main_business);
            $('#saveModal').modal('show');
        });
        $('#stock_save').click(function () {
            $('#saveModal').modal('hide');
           saveItem();
        });
    });

    function doSearch() {
        var code = $('#code').val();
        var name = $('#stock_name').val();
        loading('正在加载中...');
        $.ajax({
            type:"post",
            url:"./index.php?s=Home/Admin/searchStock",
            async:true,
            datatype:'json',
            data:{
                page:current,
                code:code,
                code_name: name
            },
            timeout:1000,
            cache:false,
            success:function (data) {
                appendHtml(data.data);
                updatePage(data.page,data.num,data.total);
                removeLoading('loading');
            },
            error: function (error) {
                removeLoading('loading');
            }
        });
    }
    function saveItem() {
        loading('正在加载中...');
        $.ajax({
            type:"post",
            url:"./index.php?s=Home/Admin/saveStock",
            async:true,
            datatype:'json',
            data:{
                id: $('#id').val(),
                name: $('#name').val(),
                org_name: $('#org_name').val(),
                address: $('#address').val(),
                main_business: $('#main_business').val()
            },
            timeout:1000,
            cache:false,
            success:function (data) {
                removeLoading('loading');
                if (data.code == 0) {
                    $('#id').val('');
                    $('#name').val('');
                    $('#org_name').val('');
                    $('#address').val('');
                    $('#main_business').val('');
                    show('更新成功', 'success');
                    doSearch();
                } else {
                    show('更新失败', 'error');
                }
            },
            error: function (error) {
                removeLoading('loading');
                show('系统错误', 'error');
            }
        });
    }
    function appendHtml(data) {
        var html = '';
        for (var i in data) {
            html += '<tr data-json=\''+ JSON.stringify(data[i]) +'\'>';
            html += '<td>' + data[i].code + '</td>';
            html += '<td>' + data[i].name + '</td>';
            html += '<td>' + data[i].org_name + '</td>';
            html += '<td>' + data[i].address + '</td>';
            html += '<td>' + data[i].list_time + '</td>';
            html += '<td>' + data[i].chairman + '</td>';
            html += '<td>' + data[i].main_business + '</td>';
            html += '<td><button class="btn btn-primary" id="save">编辑</button></td>';
            html += '</tr>';
        }
        $('#tbody').html(html);
    }
    function updatePage(page, totalpage, total, sort) {
        $("#page").paging({
            pageNo:page,
            totalPage:totalpage,
            totalSize:total,
            callback:function(num) {
                current = num;
                doSearch(num);
            }
        })
    }
</script>
</html>